package com.jair.post;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;

import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.FileEntity;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.DefaultProxyRoutePlanner;
import org.apache.http.util.EntityUtils;

public class httpClientTest {
	
	public static void postSougo(String fileName) throws ClientProtocolException, IOException{
		//HttpHost proxy = new HttpHost("127.0.0.1", 8888);
		//DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
		
		String url = "http://pic.sogou.com/pic/upload_pic.jsp";
		HttpPost httpPost = new HttpPost(url);
		httpPost.setHeader("User-Agent","Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36");
		httpPost.setHeader("Host","pic.sogou.com");
		//httpPost.setHeader("Content-Length","57970");
		httpPost.setHeader("Origin","http://pic.sogou.com");
		//httpPost.setHeader("Content-Type","multipart/form-data;");
		//httpPost.setHeader("Referer","http://pic.sogou.com/resource/pic/shitu_intro/index.html");
		httpPost.setHeader("Accept-Encoding","gzip, deflate");
		httpPost.setHeader("Accept-Language","zh-CN,zh;q=0.9");
		//httpPost.setHeader("Accept-Charset","GBK,utf-8;q=0.7,*;q=0.7");
		httpPost.setHeader("Connection","keep-alive");

		//把文件转换成流对象FileBody  
        //FileBody bin = new FileBody(new File(filePath));  
        //普通字段  重新设置了编码方式  
        //StringBody comment = new StringBody("这里是一个评论", ContentType.create("text/plain", Consts.UTF_8));  
        //StringBody comment = new StringBody("这里是一个评论", ContentType.TEXT_PLAIN);  
          
        //StringBody name = new StringBody("王五", ContentType.create("text/plain", Consts.UTF_8));  
       // StringBody password = new StringBody("123456", ContentType.create("text/plain", Consts.UTF_8));  
         // 
//        HttpEntity reqEntity = MultipartEntityBuilder.create()  
//        .addPart("media", bin)//相当于<input type="file" name="media"/>  
//        .addPart("comment", comment)  
//        .addPart("name", name)//相当于<input type="text" name="name" value=name>  
//        .addPart("password", password)  
//        .build();  
//          
//        httpPost.setEntity(reqEntity);  

		MultipartEntity mutiEntity = new MultipartEntity();
		File file = new File(fileName);

		//mutiEntity.addPart("filename",new StringBody("test21515671267051.png"));

		mutiEntity.addPart("pic_path", new FileBody(file));

		 
		CloseableHttpClient httpclient = HttpClients.createDefault();
		httpPost.setEntity(mutiEntity);
//		CloseableHttpClient httpclient = HttpClients.custom()
//		        .setRoutePlanner(routePlanner)
//		        .build();
		CloseableHttpResponse   response = httpclient.execute(httpPost);
		 HttpEntity entity = response.getEntity();  
		 String temp_img = EntityUtils.toString(entity, "UTF-8");
		 System.out.println(temp_img);
		 HttpGet httpget = new HttpGet(
			     "http://pic.sogou.com/pic/ocr/ocrOnline.jsp?query="+temp_img);
		 CloseableHttpResponse   response2 = httpclient.execute(httpget);
		 HttpEntity entity2 = response2.getEntity();  
		 System.out.println(EntityUtils.toString(entity2, "UTF-8"));
//		Response response = Request.Post("http://pic.sogou.com/pic/upload_pic.jsp")
//    			.addHeader("Cookie", "sign="+map.get("sign")+";captchaVerify="+map.get("captchaVerify"))
//		       .bodyForm(Form.form()
//		    		   .add("city", "36")
//		    		   .add("mihome", "MI0104")
//		    		   .add("productId", "2be3nojq")
//		    		   .add("day", "20170506")
//		    		   .add("time", "1012")
//		    		   .add("username", "温杰")
//		    		   .add("idCard", "142431199110267532")
//		    		   .add("phoneNo", "18519661642")
//		    		   .add("reservationId", "66")//变化
//		    		   .add("captcha", map.get("captcha"))
//		    		   .add("token", map.get("token"))
//		    		   .build())
//		       .execute();
//    	String responseStr = response.returnContent().asString();
//    	System.out.println("result="+responseStr);
	}
}
